package xushuo.dao.entity.token;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import sun.rmi.runtime.Log;
import xushuo.dao.entity.weixin.token.AccessToken;

import javax.annotation.Resource;
import java.util.List;



/**
 * Create by zhangpe0312@qq.com on 2018/2/5.
 */
@Repository
public class AccessTokenDao {

    public static Logger logger = Logger.getLogger(AccessToken.class);

    @Resource
    private SessionFactory sessionFactory;

    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    public void addAccessToken(AccessToken token) {
        this.getSession().save(token);
        logger.info("添加信息成功");
    }


    public AccessToken getAccessToken() {

        String sql = "select * from accesstoken";
        Query query = getSession().createSQLQuery(sql);
        List<AccessToken> list = query.list();

        logger.info("获取到了信息" + list.toString());

        if (list.size() != 0)
            return list.get(list.size() - 1);

        return null;
    }

    public void deleteAccessToken(AccessToken token){
        this.getSession().delete(token);
    }
}
